<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2/6/2020
 * Time: 11:44 AM
 * Note: base.php
 */

namespace app\lib;

use app\lib\enum\enum_sys;
use app\lib\model\version;
use Core\Factory;
use Core\Lib\App;
use Core\Lib\IOUnit;
use Ext\libCache;
use Ext\libConfGet;
use Ext\libCrypt;
use Ext\libErrno;
use Ext\libLock;
use Ext\libPDO;
use Ext\libQueue;
use Ext\libRedis;

/**
 * Class base
 *
 * @package app
 */
class base extends Factory
{
    public App        $app;
    public IOUnit     $io_unit;
    public \Redis     $redis;
    public libPDO     $lib_pdo;
    public libLock    $lib_lock;
    public libCrypt   $lib_crypt;
    public libConfGet $lib_conf;
    public libErrno   $lib_errno;
    public libQueue   $lib_queue;
    public libCache   $lib_cache;

    //渠道id
    public int $union_id = 10000;

    //平台ID
    public int $channel_id = 20000;

    public string $env = 'prod';

    /**
     * base constructor.
     *
     * @throws \Exception
     */
    public function __construct()
    {
        $this->app      = App::new();
        $this->lib_conf = libConfGet::new();

        //判断环境设置
        if (is_file($env_file = realpath($this->app->root_path . DIRECTORY_SEPARATOR . 'conf/.env'))) {
            $this->env = trim(file_get_contents($env_file));
        }

        //加载配置
        $this->lib_conf->load('conf/' . $this->env . '.conf');

        //加载错误码
        $this->lib_errno = libErrno::new();

        //默认操作成功，具体状态码在业务中修改
        $this->lib_errno->set(10000, 0, '操作成功');
        $this->io_unit = IOUnit::new();

        //初始化配置
        $this->init();
        $this->lively();
        $this->lively_wx();
    }

    /**
     * 初始化配置
     *
     * @throws \RedisException
     */
    public function init(): void
    {
        $this->lib_pdo = libPDO::new($this->lib_conf->use('mysql'));

        $this->lib_crypt = libCrypt::new()->bindCryptGen(KB_keygen::new());
    }

    /**
     * 失败返回
     *
     * @param int    $code
     * @param string $msg
     */
    public function fail(int $code, string $msg = '')
    {
        //从ini中填充msg
        if ('' === $msg) {
            try {
                $msg_data = $this->app->parseConf($this->app->getConfPath('msg/error.ini'), false);
            } catch (\Throwable $throwable) {
                $msg_data = [];
            }

            $msg = $msg_data[$code] ?? '操作失败';
        }

        $this->lib_errno->set($code, 1, $msg);
        IOUnit::new()->output();
        exit();
    }

    /**
     * @param string $url
     *
     * @return string
     */
    public function file_url(string $url): string
    {
        return enum_sys::DL_URL . ltrim($url, '/');
    }

    /**
     * 日活统计
     */
    public function lively()
    {
        if (!isset($this->io_unit->src_input['machine_id'])) {
            return;
        }

        $day = date("Ymd");
        $key = "stats:lively:" . $day;
        $this->redis->sAdd($key, $this->io_unit->src_input['machine_id']);
        $this->redis->expire($key, 30 * 3600);
    }

    /**
     * 微信端日活统计
     */
    public function lively_wx()
    {
        if (!isset($this->io_unit->src_input['machine_id_wx'])) {
            return;
        }

        $day = date("Ymd");
        $key = "stats:lively_wx:" . $day;
        $this->redis->sAdd($key, $this->io_unit->src_input['machine_id_wx']);
        $this->redis->expire($key, 30 * 3600);
    }


    /**
     * 过滤输入
     *
     * @param array $input_data
     */
    public function escape_base(array &$input_data): void
    {
        foreach ($input_data as $key => &$value) {
            if (is_array($value)) {
                $this->escape_base($value);
                continue;
            }
            if (is_string($value)) {
                $value = htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'UTF-8');
            }
        }
    }

}